<template>
  <div>
    <el-dialog
    :title="!row ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
      <el-form :model="dataForm" ref="dataForm" label-width="80px">
        <el-form-item label="视频">
          <el-upload
            class="upload-demo"
            :action="url"
            :limit="1"
            :on-success="videoUploadSuccess"
            :on-exceed="handleExceed"
            :file-list="fileVedioList"
            :before-upload="beforeVedioUpload">
            <el-button size="small" type="primary" @click="clickType(0)">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
            class="upload-demo"
            :action="url"
            :limit="1"
            :on-success="videoUploadSuccess"
            :on-exceed="handleExceed"
            :file-list="fileImgList"
            :before-upload="beforeAvatarUpload">
            <el-button size="small" type="primary" @click="clickType(1)">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {treeDataTranslate} from '@/utils'
  export default {
    data () {
      return {
        row: '',
        visible: false,
        url: '',
        type: 0,
        fileVedioList: [],
        fileImgList: [],
        dataForm: {}
      }
    },
    methods: {
      init (row) {
        this.row = row
        this.dataForm = {}
        this.fileVedioList= []
        this.fileImgList= []
        this.url = this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`)
        this.visible = true
        if(row) {
          this.$nextTick(() => {
            //回显
            if(this.row.vedio){
              let nameList = row.vedio.split('/');
              let name = nameList[nameList.length-1]
              this.fileVedioList.push({name,url: row.vedio})
              this.dataForm.vedio = row.vedio
            }
            if(this.row.img){
              let nameList = row.img.split('/');
              let name = nameList[nameList.length-1]
              this.fileImgList.push({name,url: row.img})
              this.dataForm.img = row.img
            }
          })
        }
      },
      clickType(type) {
        this.type = type
      },
      videoUploadSuccess (response){
        if (response.code === 0) { //状态码 成功
          if(this.type === 0) {
            this.dataForm.vedio = response.url
          }else {
            this.dataForm.img = response.url
          }
        }
      },
      beforeVedioUpload(file) {
        if (file.type !== 'video/mp4') {
          this.$message.error('只支持mp4格式！')
          return false
        }
      },
      beforeAvatarUpload (file) {
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message.error('只支持jpg、png、gif格式的图片！')
          return false
        }
      },
      handleExceed () {
        this.$message.warning(`最多上传一个视频`)
      },
      dataFormSubmit() {
        this.$emit('refreshDataList',this.dataForm,this.row)
        this.visible = false
      }
    }
  }
</script>